<?php
class router
{
    public static $load_page = false;

    public static function init()
    {
        if (!self::private_route(url::$array))
        {
            if (!self::default_route(url::$array))
            {
                self::error_route();
            }
        }
    }

    public static function private_route($url = array())
    {
        $json = file_get_contents('router/'.configuration::$router['private-route-json-name']);
        if ($json = json_decode($json) && is_array($url))
        {
            $buf = current($url);
            $url_str = '';
            $error = false;
            while ($buf !== false)
            {
                if (!empty($json[$buf]))
                {
                    $json = $json[$buf];
                    $buf = next($url);
                }
                else
                {
                    $error = true;
                }
            }

            if (!$error && is_string($json))
            {
                self::$load_page = $json;
            }
            return true;
        }
        return false;
    }

    /**
     * @static load a error pages
     * @param int $code - error code
     */
    public static function error_route($code = 404)
    {

    }

    /**
     * @static load controller-action-[id] page
     * @param array $cai = array('controller' => 'cnt', 'action' => 'act', 'id' => 'num')
     */
    public static function default_route($cai = array())
    {
        $cai['action'] = empty($cai['action']) ? 'action' : $cai['action'];

        if (!empty($cai['controller']))
        {
            if (!empty($cai['id']) && is_numeric($cai['id']))
            {
                call_user_func($cai['controller'].'::'.$cai['action'].'('.$cai['id'].')');
            }
            else
            {
                call_user_func($cai['controller'].'::'.$cai['action'].'()');
            }
        }
        else
        {
            // todo add error control
        }
    }
}
